package com.foo.DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import javax.sql.DataSource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;


public class AllRealTimeDAOImpl implements AllRealTimeDAO
{
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	MapSqlParameterSource namedParameters =null;
	String query = "";
	String ticker = "";
	int size=0;

	public void setDataSource(DataSource dataSource) 
	{
		this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
	}

	@Override
	public String selectForum() 
	{
		
		try
		{
		 query = "select distinct * from realtimestockdata Group by ticker_symbol order by ticker_symbol";
		
		 return (String) namedParameterJdbcTemplate.queryForObject(query,
					namedParameters, new RowMapper() 
			{
				public Object mapRow(ResultSet resultSet, int rowNum)
						throws SQLException {
						if (resultSet !=null)
						{
							resultSet.last();
							size=resultSet.getRow();
							resultSet.beforeFirst();
							
						}
						while(resultSet.next())
						{	
							ticker += resultSet.getString(1) + ',' +resultSet.getDouble(2)+',' ;
						}
					return ticker; 
				}
			});
		}
		catch(EmptyResultDataAccessException e)
		{
			return null;
		}
	}

}


